# coding: utf-8
import numpy as np
import pandas as pd
import argparse
import matplotlib.pyplot as plt
from sklearn.externals import joblib
from sklearn.model_selection import train_test_split

## 参数
# 文件读取路径
filename = r"F:\Resources\TEST_\train_color01.csv"
# 指定预测栏位
predict_column = "label"
# 分割数据集后测试集的比例
test_size = 0.2
# 决策树训练的最大深度(不要超过特征组合列数的一半)
max_depth = 4
# 预测标签的别名(多个后面添加key:value健值对)
class_names = {0: "G", 1: "S", 2: "D"}

# Read dataset
df = pd.read_csv(filename)
label = pd.DataFrame(df.pop(predict_column))
# label = df.pop(predict_column)
print(label)
features = df
# print(features.info(), label.info())

# Always good to set a seed for reproducibility
SEED = 22
np.random.seed(SEED)

# xtrain, xtest, ytrain, ytest = train_test_split(features, label, test_size=test_size, random_state=SEED)

# A look at the data
print("\nExample data:")

# Decision tree plot graph
import pydotplus
from IPython.display import Image
from sklearn.metrics import roc_auc_score
from sklearn.tree import DecisionTreeClassifier, export_graphviz

dt: DecisionTreeClassifier = joblib.load('F:\Resources\TEST_\DecisionTree.pkl')

# def print_graph(clf, feature_names):
#     """Print decision tree"""
#     graph = export_graphviz(
#         clf,
#         label="root",
#         proportion=True,
#         impurity=False,
#         out_file=None,
#         feature_names=feature_names,
#         class_names=class_names,
#         filled=True,
#         rounded=True)
#     graph = pydotplus.graph_from_dot_data(graph)
#     return Image(graph.create_png())
# t = DecisionTreeClassifier(max_depth=max_depth, random_state=SEED)
# t.fit(xtrain, ytrain)
# p = t.predict_proba(xtest)[:, 1]
# # print("Decision tree ROC-AUC score: %.3f" % roc_auc_score(ytest, p))
# # joblib.dump(t, "random_forest.pkl")
#
# print_graph(t, xtrain.columns)


#######################################
import numpy as np
import pandas as pd
import argparse
import matplotlib.pyplot as plt
from sklearn.externals import joblib
from sklearn.model_selection import train_test_split

# 文件读取路径
filename = r"F:\Resources\TEST_\train_color01.csv"
# 指定预测栏位
predict_column = "label"
# 预测标签的别名(多个后面添加key:value健值对)
class_names = {0: "G", 1: "S", 2: "D"}

# Read dataset
df = pd.read_csv(filename)
df.pop(predict_column)
xtest = df
print('xtest,',len(xtest))
# print(features.info(), label.info())

# Always good to set a seed for reproducibility
SEED = 22
np.random.seed(SEED)

# Decision tree plot graph
import pydotplus
from IPython.display import Image
from sklearn.tree import DecisionTreeClassifier, export_graphviz

dt: DecisionTreeClassifier = joblib.load('F:\Resources\TEST_\DecisionTree.pkl')

# def print_graph(clf, feature_names):
#     """Print decision tree"""
#     graph = export_graphviz(
#         clf,
#         label="root",
#         proportion=True,
#         impurity=False,
#         out_file=None,
#         feature_names=feature_names,
#         class_names=class_names,
#         filled=True,
#         rounded=True)
#     graph = pydotplus.graph_from_dot_data(graph)
#     return Image(graph.create_png())

p = dt.predict(xtest)
print('prediction:',type(p),len(p))
for index,i in enumerate(p):
    print('第{}行的预测标签:{}'.format(index,class_names[i]))
# print_graph(t, xtrain.columns)





